import 'package:flutter/material.dart';
import 'package:flutter_food_ordering_web/model/cart_model.dart';
import 'package:flutter_food_ordering_web/model/food_model.dart';
import 'package:flutter_food_ordering_web/res/dimens.dart';
import 'package:flutter_food_ordering_web/routers/fluro_navigator.dart';
import 'package:flutter_food_ordering_web/routers/routers.dart';
import 'package:flutter_food_ordering_web/widget/load_image.dart';
import 'package:flutter_food_ordering_web/widget/my_dialog.dart';
import 'package:provider/provider.dart';

class FoodCard extends StatefulWidget {
  final Food food;
  FoodCard(this.food);

  _FoodCardState createState() => _FoodCardState();
}

class _FoodCardState extends State<FoodCard> {
  Food get food => widget.food;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 118,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          buildImage(),
          buildTitle(),
          buildPriceInfo(),
          buildAddToShoppingCart()
        ],
      ),
    );
  }

  Widget buildImage() {
    return Image.network(
      food.image,
      fit: BoxFit.fill,
      width: 118,
      height: 124,
      loadingBuilder: (context, Widget child, ImageChunkEvent progress) {
        if (progress == null) return child;
        return Padding(
            padding: EdgeInsets.all(32),
            child: CircularProgressIndicator(
              value: progress.expectedTotalBytes != null ? progress.cumulativeBytesLoaded / progress.expectedTotalBytes : null,
            ));
      },
    );
  }

  Widget buildTitle() {
    return Container(
      padding: const EdgeInsets.only(top: 6.0),
      child: Text(
        food.name,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: Dimens.font_sp12, color: Color(0xff462708)),
      ),
    );
  }

  Widget buildPriceInfo() {
    return Container(
      padding: const EdgeInsets.only(top: 6.0, bottom: 6.0),
      child: Text(
        '\￥ ${food.price}',
        style: TextStyle(fontSize: Dimens.font_sp12, color: Color(0xff462708)),
      )
    );
  }

  Widget buildAddToShoppingCart() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Row(
          children: <Widget>[
            SizedBox(width: 22),
            Text(
              '加入餐车',
              style: TextStyle(fontSize: 11, color: Color(0xffff7f00)),
            )
          ]
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            InkWell(
              onTap: addItemToCard,
              child: LoadAssetImage('store/store_add',
                width: 12,
                height: 12,
              ),
            ),
            SizedBox(width: 21)
          ]
        ),
      ],
    );
  }

  addItemToCard() {
    debugPrint('${food.name}已经添加到购物车');
    // final snackBar = SnackBar(
    //   content: Text('${food.name} added to cart'),
    //   duration: Duration(milliseconds: 100),
    // );
    // Scaffold.of(context).showSnackBar(snackBar);

    CartModel cartModel = CartModel(food: food, quantity: 1, selected: true);
    Provider.of<Cart>(context).addItem(cartModel);
    
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return MyDialog(
          title: '温馨提示',
          content: '美食已成功加入餐车',
          okText: '继续添加',
          cancelText: '去餐车结算',
          onPressed: (isOk) {
            //去餐车界面结算
            if(!isOk) {
              NavigatorUtils.push(context, Routes.checkout);
            }
          },
        );
      }
    );
  }
}
